<?php
namespace Badmins\Controller;
use Think\Controller;
class MemberController extends Controller {
	/**
	 * 会员列表
	 */
    public function index(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$where = array();
    	$status = I('status/d');
    	if($status){
    		$where['u.status'] = $status;
    	}
    	$group_id = I('group_id/d');
    	if($group_id){
    		$where['u.group_id'] = $group_id;
    	}
    	$name = I('name');
    	if(!empty($name)){
    		$where['u.name']  = array('like', "%{$name}%");
    		$where['u.email']  = array('like',"%{$name}%");
    		$where['u.mobile']  = array('like',"%{$name}%");
    		$where['_logic'] = 'or';
    		$map['_complex'] = $where;
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$db = M('user');
    	$count = $db->where($map)->count();
    	$firstrow = ($p-1)*$listrow;
    	$join = "left join __USER_GROUP__ as ug on ug.id = u.group_id";
    	$field = "u.*,ug.name as groupname";
    	$list = lists('user as u',$join,$map,$field,"id desc",$firstrow,$listrow,2);
    	if(!empty($list)){
    		foreach ($list as $i=>$va){
    			//总消费
    			$list[$i]['exp_money'] = M('order')->where("user_id='{$va['id']}' and order_status >= '1'")->sum('order_amount');
    			
    		}
    	}
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	//会员组
    	$res = lists('user_group','','',"*","id desc",0,0,2);
    	$this->assign('res',$res);
        $this->display();
    }
    /**
     * 会员等级
     */
    public function lev(){
    	$listrow = (int)I('pageSize');
    	if(empty($listrow)){
    		$listrow = 15;
    	}
    	$p = I('pageCurrent/d')?I('pageCurrent/d'):'1';
    	$count = M('user_group')->count();
    	$firstrow = ($p-1)*$listrow;
    	$list = lists('user_group','',1,"*","id desc",$firstrow,$listrow,2);
    	if($count>$listrow){
    		$show['total'] = $count;
    		$show['listrow'] = $listrow;
    		$this->assign('show',$show);
    	}
    	$this->assign('list',$list);
    	$this->display();
    }
    
}


